package team.jaman.autosell.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import team.jaman.autosell.dao.LoggerMapper;
import team.jaman.autosell.dao.ProductMapper;
import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Manager;
import team.jaman.autosell.entity.Product;
import team.jaman.autosell.entity.ProductExample;
import team.jaman.autosell.entity.ProductExample.Criteria;
import team.jaman.autosell.service.ProductService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.PageData;
/**
 * 商品业务层实现类
 * @author Administrator
 *
 */
@Service
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private LoggerMapper loggerMapper;

	public PageData<Product> selectByExample(int page, int limit, String keyword) {
		// 创建一个ProductExample
		// ProductExample example = new ProductExample();
		//example.createCriteria().andProductNameLike("%" + keyword + "%");
		// pageHelper分页插件
		Page<Product> startPage = PageHelper.startPage(page, limit);
		// 模糊查询数据		
		List<Product> data = productMapper.selectProductByLike(keyword);
		// List<Product> data = productMapper.selectByExample(example);
		// 总记录数
		long total = startPage.getTotal();
		
		PageData<Product> pageData = new PageData<Product>(data, (int) total, limit, page);
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}


	@Override
	public CommonData updateProductById(Product product,Manager manager,Logger logger) {
		// 修改方法
		int updateByPrimaryKeySelective = productMapper.updateByPrimaryKeySelective(product);
		CommonData commonData = new CommonData();
		// 判断
		if(updateByPrimaryKeySelective > 0) {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("updateProduct");
			logger.setErrorMsg("修改成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("修改成功");
		} else {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("updateProduct");
			logger.setErrorMsg("修改失败");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("修改失败");
		}	
		return commonData;
	}


	@Override
	public CommonData deleteProductById(Integer productId,Manager manager,Logger logger) {
		// 删除方法
		int deleteByPrimaryKey = productMapper.deleteByPrimaryKey(productId);
		CommonData commonData = new CommonData();
		// 判断
		if(deleteByPrimaryKey > 0) {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("deleteProduct");
			logger.setErrorMsg("删除成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("删除成功");
		} else {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("deleteProduct");
			logger.setErrorMsg("删除失败");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("删除失败");
		}
		return commonData;
	}


	@Override
	public CommonData addProduct(Product product,Manager manager,Logger logger) {
		// 增加方法
		int insertSelective = productMapper.insertSelective(product);
		CommonData commonData = new CommonData();
		// 判断
		if(insertSelective > 0) {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("addProduct");
			logger.setErrorMsg("增加成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("增加成功");
		} else {
			logger.setManagerId(manager.getManagerId());
			logger.setOperatorContent("addProduct");
			logger.setErrorMsg("增加失败");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			commonData.setMsg("增加失败");
		}
		return commonData;
	}

}
